package com.barradata.app0624.modules.idcard

import android.content.Context
import android.content.Intent
import com.barradata.R
import com.barradata.app0624.api.ApiService
import com.barradata.app0624.modules.web.EventType
import com.barradata.app0624.modules.web.WebCallbackEvent
import com.barradata.app0624.validate.isIdCardNumber
import com.barradata.common.base.BaseActivity
import com.barradata.common.rxbinding.clickV
import com.barradata.common.rxbus.RxBus
import com.barradata.common.utils.ToastUtils
import com.example.hfiveandaplication.bean.IdentityData
import com.jakewharton.rxbinding3.widget.textChanges
import io.reactivex.rxkotlin.Observables
import kotlinx.android.synthetic.main.idcard_confirm_activity.*
import kotlinx.android.synthetic.main.idcard_confirm_activity.next_button
import kotlinx.android.synthetic.main.idcard_confirm_activity.toolbar

/**
 * Author:    WuNan
 * Email:     wunan@barradata.com
 * Created:   2019/7/9 13:45
 **/
class IdCardConfirmActivity : BaseActivity() {
    companion object {
        private const val IDENTITY_BEAN_KEY = "IDENTITY_BEAN_KEY"
        private const val ID_CARD_KEY = "ID_CARD_KEY"
        private const val ID_CARD_WITH_PORTRAIT_KEY = "ID_CARD_WITH_PORTRAIT_KEY"
        private const val PASS_KEY = "PASS_KEY"

        fun actionStart(context: Context, pass: Int, identityBean: IdentityData.IdentityBean,
                        idCardImageInfo: IdentityData.ImageInfoBean,
                        idCardWithPortraitImageInfo: IdentityData.ImageInfoBean) {
            val intent = Intent(context, IdCardConfirmActivity::class.java)
            intent.putExtra(IDENTITY_BEAN_KEY, identityBean)
            intent.putExtra(PASS_KEY, pass)
            intent.putExtra(ID_CARD_KEY, idCardImageInfo)
            intent.putExtra(ID_CARD_WITH_PORTRAIT_KEY, idCardWithPortraitImageInfo)
            context.startActivity(intent)
        }

        fun actionStart(context: Context) {
            val intent = Intent(context, IdCardConfirmActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var identityBean: IdentityData.IdentityBean? = IdentityData.IdentityBean()
    private var pass = 0
    private var idCardImageInfo: IdentityData.ImageInfoBean? = null
    private var idCardWithPortraitImageInfo: IdentityData.ImageInfoBean? = null

    override fun getLayoutId() = R.layout.idcard_confirm_activity

    override fun initView() {
        setupActionBar(toolbar)
        fetchData()
        fillData()
        Observables.combineLatest(id_name_et.textChanges(), id_number_et.textChanges(), id_birthday_et.textChanges()) { idName, idNumber, idBirthday ->
            idName.isNotEmpty() && idNumber.isNotEmpty() && idBirthday.isNotEmpty()
        }.subscribe {
            if (it) {
                next_button.alpha = 1f
                next_button.isEnabled = true
            } else {
                next_button.alpha = 0.55f
                next_button.isEnabled = false
            }
        }.autoClean()
        next_button.clickV().filter { validateFormData() }.subscribe {
            requestUpdateUserInfo()
        }.autoClean()

    }

    override fun onBackPressed() {
        super.onBackPressed()
        RxBus.post(WebCallbackEvent(EventType.FROM_CANCEL))
    }

    private fun validateFormData(): Boolean {
        identityBean?.realName = id_name_et.text.toString()
        identityBean?.identityCardNum = id_number_et.text.toString()
        identityBean?.birthday = id_birthday_et.text.toString()
        if (identityBean == null) {
            return false
        }
        if (!identityBean!!.identityCardNum.isIdCardNumber()) {
            ToastUtils.showShort(R.string.auth_error_idcard_number)
            return false
        }
        return true
    }

    private fun requestUpdateUserInfo() {
        ApiService.updateUserInfo(pass, identityBean!!,
                listOf(idCardImageInfo!!, idCardWithPortraitImageInfo!!)).subscribe({
            if (it.status == 200) {
//                 跳转
                RxBus.post(WebCallbackEvent(EventType.FROM_IDCARD))
                finish()
            } else {
                ToastUtils.showShort(it.message)
            }
        }, {
            it.printStackTrace()
            ToastUtils.showShort(R.string.network_error)
        }).autoClean()

    }

    private fun fillData() {
        id_name_et.setText(identityBean?.realName)
        id_number_et.setText(identityBean?.identityCardNum)
        id_birthday_et.setText(identityBean?.birthday)
    }

    private fun fetchData() {
        val temp = intent.getSerializableExtra(IDENTITY_BEAN_KEY)
        if (temp is IdentityData.IdentityBean) {
            identityBean = temp
        }
        pass = intent.getIntExtra(PASS_KEY, 0)
        idCardImageInfo = intent.getParcelableExtra(ID_CARD_KEY)
        idCardWithPortraitImageInfo = intent.getParcelableExtra(ID_CARD_WITH_PORTRAIT_KEY)

    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0, R.anim.fade_out)
    }
}